package com.yhy.wxzn.wms.rule;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yhy.wxzn.wms.entity.WmsAsnDetail;
import com.yhy.wxzn.wms.entity.WmsPickDetail;
import com.yhy.wxzn.wms.service.IWmsAsnDetailService;
import com.yhy.wxzn.wms.service.IWmsPickDetailService;
import org.jeecg.common.handler.IFillRuleHandler;
import org.jeecg.common.util.SpringContextUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Auther: 97354
 * @Date: 2023/11/1 10:56
 * @Description: WmsLineNoRule
 * @Version 1.0.0
 */
public class WmsLineNoRule implements IFillRuleHandler {

    @Override
    public Object execute(JSONObject params, JSONObject formData) {
        int lineNo = 10;
        // 根据formData的值的不同，生成不同的订单号
        String asnId = formData.getString("asnId");
        String pickId = formData.getString("pickId");
        if (!StringUtils.isEmpty(asnId)) {
            IWmsAsnDetailService wmsAsnDetailService = SpringContextUtils.getApplicationContext().getBean(IWmsAsnDetailService.class);
            if(!ObjectUtils.isEmpty(wmsAsnDetailService)){
                LambdaQueryWrapper<WmsAsnDetail> query = new LambdaQueryWrapper();
                query.eq(WmsAsnDetail::getAsnId,asnId);
                query.orderByDesc(WmsAsnDetail::getLineNo);
                List<WmsAsnDetail> details = wmsAsnDetailService.list(query);
                if(!CollectionUtils.isEmpty(details)){
                    lineNo = details.get(0).getLineNo()==0?10:details.get(0).getLineNo()+10;
                }
            }
        }
        if (!StringUtils.isEmpty(pickId)) {
            IWmsPickDetailService wmsPickDetailService = SpringContextUtils.getApplicationContext().getBean(IWmsPickDetailService.class);
            if(!ObjectUtils.isEmpty(wmsPickDetailService)){
                LambdaQueryWrapper<WmsPickDetail> query = new LambdaQueryWrapper();
                query.eq(WmsPickDetail::getPickId,pickId);
                query.orderByDesc(WmsPickDetail::getLineNo);
                List<WmsPickDetail> details = wmsPickDetailService.list(query);
                if(!CollectionUtils.isEmpty(details)){
                    lineNo = details.get(0).getLineNo()==0?10:details.get(0).getLineNo()+10;
                }
            }
        }
        return lineNo;
    }
}
